package com.zmx.patientend.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.zmx.patientend.R;
import com.zmx.patientend.pojo.Department;
import com.zmx.patientend.pojo.RegistrationDate;

import java.util.List;

public class RegistrationDateAdapter extends RecyclerView.Adapter<RegistrationDateAdapter.ViewHolder>{

    private List<RegistrationDate> lists;
    private Context context;

    //内部类，继承自RecyclerView.ViewHolder
    //接收一个View ，通常就是RecyclerView子项的最外层布局，
    //所以就可以用findViewById 来获取控件
    static class  ViewHolder extends RecyclerView.ViewHolder{
        TextView TextName,toweer;
        public ViewHolder(View View) {
            super(View);

            TextName = View.findViewById(R.id.Tv);
            toweer = View.findViewById(R.id.toweer);
        }
    }
    //将要展示的数据传递进来，
    public RegistrationDateAdapter(List<RegistrationDate> lists, Context context){
        this.lists = lists ;
        this.context = context;
    }
    //将fruit_item 布局加载进来，然后创建一个ViewHolder实例，
    //将加载的布局传给ViewHolder的构造函数中。就可以获取布局中的控件
    @Override
    public RegistrationDateAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_registration_date,parent,false);
        RegistrationDateAdapter.ViewHolder holder = new RegistrationDateAdapter.ViewHolder(view);
        return holder;

    }
    //这个方法会在屏幕滚动的时候执行
    // 通过position获取到Fruit 的实例，然后给布局上的控件进行赋值，
    @Override
    public void onBindViewHolder(RegistrationDateAdapter.ViewHolder holder, final int position) {

        holder.toweer.setText(lists.get(position).getToWeek());
        holder.TextName.setText(lists.get(position).getDay());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

              //一定要刷新适配器 当条目发生改变这是必须的
                getListener.onClick(position);
                notifyDataSetChanged();
                Log.e("点击了","点击了"+lists.get(position).getDay());
                op.setOptiondateListener(lists.get(position).getDay());

            }
        });

        //        如果下标和传回来的下标相等 那么确定是点击的条目 把背景设置一下颜色
        if (position == getmPosition()) {

            holder.TextName.setTextColor(context.getColor(R.color.there));
            holder.toweer.setTextColor(context.getColor(R.color.there));
        }else{
//            否则的话就全白色初始化背景
            holder.TextName.setTextColor(Color.BLACK);
            holder.toweer.setTextColor(Color.BLACK);
        }

    }

    //他用于告诉RecyclerView有多少子项，直接返回数据源的长度就行了
    @Override
    public int getItemCount() {
        return lists.size();
    }

    public interface GetListener {

        void onClick(int position);
    }

    private RegistrationDateAdapter.GetListener getListener;
    public void setGetListener(RegistrationDateAdapter.GetListener getListener) {
        this.getListener = getListener;
    }
    private  int mPosition;

    public int getmPosition() {
        return mPosition;
    }

    public void setmPosition(int mPosition) {
        this.mPosition = mPosition;
    }

    public OptiondateListener op;
    public interface OptiondateListener{
        void setOptiondateListener(String day);
    }

    public void setOptiondateListener(OptiondateListener o){
        this.op = o;
    }

}
